package com.rockbb.common.sso.session;

import java.util.Date;
import java.util.Map;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
import com.rockbb.common.sso.config.SSOConfig;

public class MemCachedProxy
{
	// create a static client as most installs only need a single instance
	protected static MemCachedClient mcc = new MemCachedClient();

	// set up connection pool once at class load
	static
	{
		// server list and weights
		String[] servers = { SSOConfig.getValue("memcached.host") };
		Integer[] weights = { 2 };

		// grab an instance of our connection pool
		SockIOPool pool = SockIOPool.getInstance();

		// set the servers and the weights
		pool.setServers(servers);
		pool.setWeights(weights);

		// set some basic pool settings
		// 5 initial, 5 minutes, and 250 max connections
		// and set the max idle time for a connection to 6 hours
		pool.setInitConn(5);
		pool.setMinConn(5);
		pool.setMaxConn(250);
		pool.setMaxIdle(1000 * 60 * 60 * 6);

		// set the sleep for the maintain thread. it will wake up every x
		// seconds and
		// maintain the pool size
		pool.setMaintSleep(30);

		// set some TCP settings
		// disable nagle
		// set the read timeout to 3 secs
		// and don’t set a connect timeout
		pool.setNagle(false);
		pool.setSocketTO(3000);
		pool.setSocketConnectTO(0);

		// initialise the connection pool
		pool.initialize();

		// lets set some compression on for the client
		// compress anything larger than 64k

	}

	private MemCachedProxy()
	{
	}

	public static boolean exists(String key)
	{
		return mcc.keyExists(key);
	}

	public static boolean set(String key, Object value)
	{
		return mcc.set(key, value);
	}

	public static boolean set(String key, Object value, Date expire)
	{
		return mcc.set(key, value, expire);
	}

	public static boolean add(String key, Object value)
	{
		return mcc.add(key, value);
	}

	public static boolean add(String key, Object value, Date expire)
	{
		return mcc.add(key, value, expire);
	}

	public static boolean replace(String key, Object value)
	{
		return mcc.replace(key, value);
	}

	public static boolean replace(String key, Object value, Date expire)
	{
		return mcc.replace(key, value, expire);
	}

	public static boolean delete(String key)
	{
		return mcc.delete(key);
	}

	public static boolean setInt(String key, long value)
	{
		return mcc.storeCounter(key, value);
	}

	public static long getInt(String key)
	{
		return mcc.getCounter(key);
	}

	public static long incr(String key, long value)
	{
		return mcc.incr(key, value);
	}

	public static long decr(String key, long value)
	{
		return mcc.decr(key, value);
	}

	public static Object get(String key)
	{
		return mcc.get(key);
	}

	public static Object[] getArray(String[] keys)
	{
		return mcc.getMultiArray(keys);
	}

	public static Map<String, Object> getMap(String[] keys)
	{
		return mcc.getMulti(keys);
	}

	public static boolean flush()
	{
		return mcc.flushAll();
	}

	// from here on down, you can call any of the client calls
	public static void main(String[] args)
	{
		mcc.set("foo", "This is a test String");
		String bar = (String) mcc.get("foo");
		Map<String, Map<String, String>> map = mcc.stats();
		System.out.println(bar);
	}
}
